<?php
/**
 * Created by IntelliJ IDEA.
 * User: wuzesheng
 * Date: 4/15/14
 * Time: 5:28 PM
 */
$count=exec('ps -ef | grep ffmpeg | wc -l');
if(is_int($count) && $count>4){
  exit();
}
//ini_set('max_execution_time', '0');
//ini_set('memory_limit', '2000M');
require_once '/mnt/default/crontabscript/examples/EasyMySQLi.inc.php';
$db = new EasyMySQLi('localhost', 'root', 'root', 'madv_db1');
try {
  $result=$db->querySingleRow('select * from file_transcode WHERE status = 0 and outputpath="/mnt/default/v.madv360.com/php/uploads/files/"');
  if(!empty($result)){
    if(!empty($result["filename"])){
      $db->queryNoResult('update file_transcode set status=1,starttime="'.date("Y-m-d H:i:s").'" where id=?',$result["id"]);//任务开始状态1
      //$object_name=$result['filename']."_".$result['resolution'].'_'.$result['fps'].'_'.$result['videocodec'].'_'.$result['bitrate'].'.mp4';
      //exec(escapeshellcmd('/usr/local/bin/ffmpeg -r '.$result['fps'].' -y -i '.$result['url'].$result['filename'].'.mp4 -acodec '.$result['audiocodec'].' -s '.$result['resolution'].' -b:v '.$result['bitrate'].' -c:v '.$result['videocodec'].' '.$result['outputpath'].$object_name));
      $object_name=$result['filename']."_".$result['resolution'].'_'.$result['videocodec'].'_'.$result['bitrate'].'.mp4';
      if(is_file($result["outputpath"].$result["filename"].".mp4")){
        exec(escapeshellcmd('/usr/local/bin/ffmpeg -y -i '.$result['outputpath'].$result['filename'].'.mp4 -acodec '.$result['audiocodec'].' -s '.$result['resolution'].' -b:v '.$result['bitrate'].' -c:v '.$result['videocodec'].' '.$result['outputpath'].$object_name));
      }else{
        exec(escapeshellcmd('/usr/local/bin/ffmpeg -re -y -i '.$result['url'].$result['filename'].'.mp4 -acodec '.$result['audiocodec'].' -s '.$result['resolution'].' -b:v '.$result['bitrate'].' -c:v '.$result['videocodec'].' '.$result['outputpath'].$object_name));
      }
    }
  }
}catch (MySQLiQueryException $ex) {
    //echo "Something went wrong: ".$ex->getMessage();
}
//exec(escapeshellcmd("/usr/local/bin/ffmpeg -i /mnt/default/20160715-1.mp4 -y -f image2 -t 0.001 ".escapeshellarg("test.png")));
//$cc=exec('/usr/local/bin/ffmpeg -i /mnt/default/20160715-1.mp4');
//$img_info=getimagesize('test.png');print_r($img_info);
//exit;
if(empty($object_name) || !is_file($result['outputpath'].$object_name)){
  exit('no file');
}else{
  echo $object_name;
  $db->queryNoResult('update file_transcode set status=2,middletime="'.date("Y-m-d H:i:s").',md5='.md5_file($result['outputpath'].$object_name).',filesize='.filesize($result['outputpath'].$object_name).'" where id=?',$result["id"]);//转码完成状态2
}

require("/mnt/default/crontabscript/bootstrap.php");

// Construct the FDS client
$access_key = "5411744243597";
$access_secret = "hu/XTi7mhl1j+pL5qkQGnw==";
$credential = new \FDS\credential\BasicFDSCredential($access_key, $access_secret);
$fds_config = new \FDS\FDSClientConfiguration();
$fds_config->setRegionName("awsbj0");
$fds_config->enableHttps(true);
$fds_config->enableCdnForUpload(false);
$fds_config->enableCdnForDownload(true);

$fds_client = new \FDS\GalaxyFDSClient($credential, $fds_config);
$bucket_name = "madv360";
// Put an object
//$object_content = file_get_contents($result['outputpath'].$object_name);
//$result2 = $fds_client->putObject($bucket_name, $object_name, $object_content);
//print_r($result2);
//$object_name = "20160715-1_2304x1152_libx264_10000000.mp4";
//$result['outputpath']="/mnt/default/v.madv360.com/php/uploads/files/";
//$result["id"]=345;
use FDS\model\UploadPartResult;
$result2 = $fds_client->initMultipartUpload($bucket_name, $object_name);
$uploadId = $result2->getUploadId();
$uploadPartResultArray = array();
$handle = fopen($result['outputpath'].$object_name, "rb");
if (FALSE === $handle) {
    exit("Failed to open stream to URL");
}
$i=1;
while (!feof($handle)) {
    $contents = fread($handle, 10000000);
    //echo $contents;
    $uploadPartResult = $fds_client->uploadPart($bucket_name,$object_name, $uploadId, $i++, $contents);
    array_push($uploadPartResultArray, $uploadPartResult);
}
fclose($handle);
$uploadPartResultList = new \FDS\model\UploadPartResultList();
$uploadPartResultList->setUploadPartResultList($uploadPartResultArray);
$fds_client->completeMultipartUpload($bucket_name, $object_name,$uploadId, null, $uploadPartResultList);

$fds_client->setPublic($bucket_name,$object_name);
// Check the object existence
$exist = $fds_client->doesObjectExist($bucket_name, $object_name);
echo ($exist);
if($exist){
  $db->queryNoResult('update file_transcode set status=3,endtime="'.date("Y-m-d H:i:s").'" where id=?',$result["id"]);//上传完成状态3
  $db->queryNoResult('update file set status=2 where filename=?',$result["filename"]);//修改状态
}
